﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ANGFLib;

namespace ANGF
{
    public partial class FormSchenario : Form
    {
		private MyXmlDoc[] list;
		public FormSchenario()
        {
            InitializeComponent();
			var temp = new List<MyXmlDoc>();
			foreach (var s in Scenarios.Modules)
			{
				if (!s.startupModule) continue;
				temp.Add(s);
			}
			list = temp.ToArray();

			// ソートする
			Array.Sort(list, (x, y) =>
			{
				long xTime = SystemFile.GetTimeStamp(x.id).ToBinary();
				long yTime = SystemFile.GetTimeStamp(y.id).ToBinary();
				if (xTime > yTime) return -1;
				if (xTime < yTime) return 1;
				return 0;
			});
		}

        private void FormSchenario_Load(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            foreach (var s in list)
            {
                this.listBox1.Items.Add(s.name);
            }
            if (this.listBox1.Items.Count > 0) this.listBox1.SelectedIndex = 0;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex >= 0)
            {
                SystemFile.SetLastPlayDateTime(GetSelected().id);
                SystemFile.Save();
            }
            this.DialogResult = DialogResult.OK;
            Close();
        }
		public MyXmlDoc GetSelected()
		{
			return (MyXmlDoc)list[this.listBox1.SelectedIndex];
		}

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedIndex >= 0)
                buttonOK_Click(sender, e);
            // 単に選択されていないなら何もしない
        }
    }
}
